package main

import (
	"bytes"
	"fmt"
	"io"
	"net"
	"strings"
	"sync"
	"time"
)

type Server struct {
	Ip   string
	Port int
	//onlineUser list
	OnlineMap map[string]*User
	mapLock   sync.RWMutex
	//Message channel
	Message chan string
}

// NewService 创建一个服务器
func NewService(ip string, port int) *Server {
	server := &Server{
		Ip:        ip,
		Port:      port,
		OnlineMap: make(map[string]*User),
		Message:   make(chan string),
	}
	return server
}

// ListenMessage 监听公用管道
func (this *Server) ListenMessage() {
	for {
		msg := <-this.Message
		this.mapLock.Lock()
		for _, cli := range this.OnlineMap {
			cli.C <- msg
		}
		this.mapLock.Unlock()
	}
}

// BroadCast 发送广播消息
func (this *Server) BroadCast(user *User, msg string) {
	sendMsg := "{" + user.Addr + "} " + user.Name + " say: " + msg
	this.Message <- sendMsg
}

// Handler 处理连接消息
func (this *Server) Handler(conn net.Conn) {
	//新用户连接
	user := NewUser(conn, this)
	//用户上线
	user.Online()

	//监听用户消息
	isLive := make(chan bool)

	//用协程监听用户连接信息并发送到广播
	go func() {
		buf := make([]byte, 4096)
		//累计信息
		var msgBuffer []byte
		for {
			//读取客户端发送的消息
			n, err := conn.Read(buf)
			//连接错误或关闭
			if n == 0 {
				user.Offline()
				return
			}
			if err != nil && err != io.EOF {
				fmt.Println("conn.Read error:", err)
				return
			}

			// 累计读取的信息
			msgBuffer = append(msgBuffer, buf[:n]...)

			// 判断是否输入enter结束输入
			for {
				// 查找换行符
				index := bytes.IndexAny(msgBuffer, "\r\n")
				// 未查到换行符即输入未完成
				if index == -1 {
					break
				}

				// 输入完成，拼接传输信息
				msg := string(msgBuffer[:index])

				// 移除空格或/n/r
				msg = strings.TrimSpace(msg)

				// 避免空信息
				if len(msg) > 0 {
					user.DoMessage(msg)
				}

				// 移除已处理的信息
				msgBuffer = msgBuffer[index+1:]

				// 发送活跃
				isLive <- true
			}
		}
	}()

	//监听用户是否活跃
	for {
		select {
		case <-isLive:
			//当前用户活跃，重置定时器
			//不做任何操作
		case <-time.After(time.Second * 300):
			//已经超时
			user.SendMsg("you are out of time")
			// 关闭管道以及连接
			close(user.C)
			err := user.conn.Close()
			if err != nil {
				fmt.Println("conn close error:", err)
				return
			}
			//关闭handler
			return
		}
	}
}

// Start 处理连接
func (this *Server) Start() {
	//socket listen
	listen, err := net.Listen("tcp", fmt.Sprintf("%s:%d", this.Ip, this.Port))
	if err != nil {
		fmt.Println("net.Listen error:", err)
		return
	}
	//defer 用来处理服务关闭后的方法
	defer func(listen net.Listener) {
		err := listen.Close()
		if err != nil {
			fmt.Println("listen.Close error:", err)
		}
	}(listen)

	// 启动服务监听
	go this.ListenMessage()

	for {
		//accept
		conn, err := listen.Accept()
		if err != nil {
			fmt.Println("listen.Accept error:", err)
			return
		}
		fmt.Println("connection success :", conn.RemoteAddr().String())
		//do handler
		go this.Handler(conn)
	}
}
